/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;

class LuaJSFightConst {
    public static final int LOOP_NUMBER = 40000;
    public static final int TIMES_KILO_MILLISECONDS = 1000;
    public static final int BENCHMARK_COUNT = 120;
    public static final int NUM_1 = 1;
    public static final int NUM_2 = 2;
    public static final int NUM_3 = 3;
    public static final int NUM_4 = 4;
    public static final int NUM_5 = 5;
    public static final int NUM_6 = 6;
    public static final int NUM_7 = 7;
}

interface ArkTools {
    int timeInUs(int args);
}

class LuaJSFight {
    
    public void deBugLog(String str){
        System.out.println(str);
    }
    
    public void run(){
        // System.out.println("Hello world");

        final int m = LuaJSFightConst.LOOP_NUMBER;
        List<String> strItems = new ArrayList<String>();
        Runnable addStrElement = ()->{
            int j = 0;
            while (j < m) {
                strItems.add("digit" + j);
                j = j + 1;
            }
            // System.out.println(strItems.get(0) + "\n" + strItems.get(m/2) + "\n" + strItems.get(m - 1));
        };
        addStrElement.run();

        final int n = LuaJSFightConst.LOOP_NUMBER;
        List<Integer> items = new ArrayList<>();
        Runnable addElement = ()->{
            int i = 0;
            while (i < n) {
                items.add(i);
                i += 1;
            }
            // System.out.println(items.get(0) + "\n" + items.get(n/2) + "\n" + items.get(n - 1));
        };
        addElement.run();

        BiFunction<int[], Function<Integer, Boolean>, Integer> firstWhere = (list, fn)->{
            for (int x : list) {
                if (fn.apply(x)) {
                    return x;
                }
            }
            return 0;
        };

        int[] nums = {LuaJSFightConst.NUM_1,LuaJSFightConst.NUM_2,LuaJSFightConst.NUM_3,LuaJSFightConst.NUM_4,LuaJSFightConst.NUM_5,LuaJSFightConst.NUM_6,LuaJSFightConst.NUM_7};
        Function<Integer, Boolean> isEven = (Integer x) -> {
            return (x & 1) == 0;
        };
        int firstEven = firstWhere.apply(nums,isEven);
        // System.out.println("First even:" + firstEven);
    }
}

/*
 * @State
 * @Tags Jetstream2
 */
class Benchmark{
    
    public static void main(String[] args) {
        Benchmark benchmark = new Benchmark();
        benchmark.runIteration();
    }

    /*
   * @Benchmark
   */
    public void runIteration(){
        LuaJSFight lua = new LuaJSFight();
        long before = System.nanoTime();
        for (int i = 0; i < LuaJSFightConst.BENCHMARK_COUNT; i++) {
            lua.run();
        }
        long after = System.nanoTime();
        System.out.println("LuaJSFight: ms = " + (double)(after - before)/1000000);    
    }
}